const mysql2 = require('mysql2');
const Joi = require('@hapi/joi');
const dateFormat = require('dateformat');
const CryptoJS = require("crypto-js");

const appKey = "28127664";
const signSecret = " 4c7d682c552b9dd06ca948f68902f61f";
const password = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(signSecret, signSecret));
// 云函数mysql数据库链接 
let poolObj = mysql2.createConnection({
    host: 'dataproxy-internal.m.mappcloud.com',
    port: 3306,
    user: appKey,
    password: password,
    database: 'test_' + appKey,
    waitForConnections: true,
    connectionLimit: 200,
    pool: {
        idle: 50,  //链接控制时间
    },
    timezone: '+08:00', //东八区
});


//  测试
exports.test1 = async (param) => {
    console.log('sharedeatil-model.test1')
    // const conn = poolObj.promise();
    // // sql 数据插入
    // let sql = `DELETE FROM sharedetail`;
    // let data = await conn.query(sql);
    // return { success: true, code: 0, data: data };
    // await this.createOne(param)
    await this.updateIsNewVipByShareMixnickAndAcceptMixnick(param)
}

/**
 *  添加一条数据
 * @param {string} param.mixnick 混淆昵称
 * @param {string} param.nickname 用户昵称
 * @param {string} param.shared_mixnick 被分享人混淆昵称
 * @param {string} param.shared_nickname 被分享人昵称
 * @param {string} param.shared_img 被分享人头像
 * @param {number} param.vipstate 会员状态，0，不是会员，1，老会员，2，新会员
 * @param {number} param.rule_id 规则id
 */
exports.createOne = async (param) => {
    try {
        console.log(param)
        const conn = poolObj.promise();
        // sql 数据插入
        let sql = `INSERT INTO sharedetail (mixnick,nickname,shared_mixnick,shared_nickname,shared_img,vipstate,rule_id,gmt_create) 
        VALUES('${param.mixnick}','${param.nickname}','${param.shared_mixnick}','${param.shared_nickname}','${param.shared_img}',${param.vipstate},'${param.rule_id}',NOW())`;
        let data = await conn.query(sql);
        return data[0];
    }
    catch (e) {
        console.log(e)
    }

}

/**
 * 根据分享人和被分享查询是否存在
 * @param {string} param.mixnick 混淆昵称
 * @param {string} param.shared_mixnick 被分享人混淆昵称
 * @param {number} param.rule_id 规则id
 */
exports.findCountByShareAndAccept = async (param) => {
    try {
        console.log('findCountByShareAndAccept', param)
        const conn = poolObj.promise();
        let sql = `select count(1) as count from sharedetail where mixnick = '${param.mixnick}' and shared_mixnick = '${param.shared_mixnick}' and rule_id = ${param.rule_id}`;
        let data = await conn.query(sql);
        return data[0][0].count;
    }
    catch (e) {
        console.log(e);
    }

}

/**
 * 修改会员状态
 * @param {number} param.vipstate 会员状态，0，不是会员，1，老会员，2，新会员
 * @param {string} param.mixnick 混淆昵称
 * @param {string} param.shared_mixnick 被分享人混淆昵称
 * @param {number} param.rule_id 规则id
 */
exports.updateIsNewVipByShareMixnickAndAcceptMixnick = async (param) => {
    console.log(param,'ssssssssssssssssssssssssssssssssss');
    try {
        const conn = poolObj.promise();
        let sql = `update sharedetail set vipstate = ${param.vipstate},gmt_modified = now() where mixnick = '${param.mixnick}' and shared_mixnick = '${param.shared_mixnick}' and rule_id = ${param.rule_id}`;
        let data = await conn.query(sql);
        return data[0];
    }
    catch (e) {
        console.log(e)
    }

}


/**
 * 根据分享人查询全部的数据
 * @param {string} param.mixnick 混淆昵称
 * @param {number} param.rule_id 规则id
 */
exports.findAllByShare = async (param) => {
    try {
        console.log(param,'sssssssssssssssssssssssssss')

        const conn = poolObj.promise();
        let sql = `SELECT
			DATE_FORMAT(gmt_create, '%Y-%m-%d') AS date,
			DATE_FORMAT(gmt_modified, '%Y-%m-%d') AS memberTime,
			
			mixnick,
			shared_mixnick,
			vipstate AS vip
		
		FROM
			sharedetail
		WHERE
			mixnick = '${param.mixnick}'
			or
		shared_mixnick = '${param.mixnick}'
		AND rule_id = ${param.rule_id}
		ORDER BY
			gmt_modified desc`;

        let data = await conn.query(sql);
        // console.log(data, '7777777777777777777777777777777777');
        return data[0];
    }
    catch (e) {
        console.log(e);
    }

}


/**
 * 查询分享成功的用户列表,
 * @param {string} param.mixnick 混淆昵称
 * @param {number} param.rule_id 规则id
 * @param {number} param.vipstate 会员状态，0，不是会员，1，老会员，2，新会员
 */
exports.findAllByShareSuccess = async (param) => {
    try {
        const conn = poolObj.promise();
        let sql = `SELECT
                    shared_mixnick,
                    shared_nickname,
                    shared_img
                FROM
                sharedetail
                WHERE
                    mixnick = '${param.mixnick}'
                and
                rule_id = ${param.rule_id}
                and
                vipstate = ${param.vipstate}
                ORDER BY
                gmt_create desc`;
        // if (param.vipstate != 2) {
        //     sql = `SELECT
        //         shared_img
        //     FROM
        //     sharedetail
        //     WHERE
        //         mixnick = '${param.mixnick}'
        //     and
        //     rule_id = ${param.rule_id}
        //     ORDER BY
        //     gmt_create desc`
        // }

        let data = await conn.query(sql);
        return data[0];
    }
    catch (e) {
        console.log(e);
    }

}

// /**
//  * 根据分享人和被分享查询是否存在,
//  * @param {string} param.mixnick 混淆昵称
//  * @param {number} param.rule_id 规则id
//  * @param {number} param.vipstate 会员状态，0，不是会员，1，老会员，2，新会员
//  */
// exports.findCountByMixnickAndStatus = async (param) => {
//     try {
//         const conn = poolObj.promise();
//         let sql = `select count(1) as count from sharedetail where mixnick = '${param.mixnick}' and vipstate = ${param.vipstate} and rule_id = ${param.rule_id}`;
//         if (param.vipstate != 2) {
//             sql = `select count(1) as count from sharedetail where mixnick = '${param.mixnick}' and vipstate in (0,1) and rule_id = ${param.rule_id}`;
//         }
//         let data = await conn.query(sql);
//         console.log(data, '7777777777777777777777777777777777');
//         return data[0][0].count;
//     }
//     catch (e) {
//         console.log(e);
//     }
// }

// /**
//  * 根据分享人和ruleId查询该用户是否在表里是
//  * @param {string} param.mixnick 混淆昵称
//  * @param {number} param.rule_id 规则id
//  */
// exports.findCountByMixnickAndRuleid = async (param) => {
//     try {
//         const conn = poolObj.promise();
//         let sql = `select count(1) as count from sharedetail where mixnick = '${param.mixnick}'  and rule_id = ${param.rule_id}`;
//         let data = await conn.query(sql);
//         console.log(data, '7777777777777777777777777777777777');
//         return { success: true, code: 0, data: data };
//     }
//     catch (e) {
//         console.log(e);
//     }

// }



